package com.zhangzhan.blog_service.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.base.constant.StatusCode;
import com.zhangzhan.base.utils.CopyBean;
import com.zhangzhan.blog_model.entity.Article;
import com.zhangzhan.blog_model.entity.Category;
import com.zhangzhan.blog_model.vos.GetCategoryVo;
import com.zhangzhan.blog_service.mapper.CategoryMapper;
import com.zhangzhan.blog_service.service.ArticleService;
import com.zhangzhan.blog_service.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author zhangzhan
 * @since 2024-01-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private ArticleService articleService;

    /**
     * @return java.util.List<com.zhangzhan.admin_model.vos.GetCategoryVo>
     * @description 插叙文章分类
     * @author zhangyiheng
     * @date 2024/1/31 22:05
     */
    @Override
    public List<GetCategoryVo> getCategoryList() {
        //求只展示有发布正式文章的分类②必须是正常状态的分类

        List<Article> articles = articleService.list(Wrappers.<Article>lambdaQuery()
                .eq(Article::getStatus, StatusCode.ND)
                .eq(Article::getDelFlag, StatusCode.ND));
        List<Long> categoryIds = articles.stream().map(Article::getCategoryId).distinct().collect(Collectors.toList());

        return CopyBean.copyBeanList(this.listByIds(categoryIds)
                .stream().filter(po->StatusCode.S_ND.equals(po.getStatus())).collect(Collectors.toList())
                , GetCategoryVo.class);
    }
}
